// 统计指定目录中文件总大小。要考虑目录中还有子目录的情况。可以同步编码,异步更好

var fs = require('fs')
//遍历读取文件
function readFile (path, filesList) {
  files = fs.readdirSync(path);//需要用到同步读取
  files.forEach(walk);
  function walk (file) {
    states = fs.statSync(path + '/' + file);
    if (states.isDirectory()) {
      readFile(path + '/' + file, filesList);
    }
    else {
      var obj = {}
      obj.size = states.size;//文件大小，以字节为单位
      obj.name = file;//文件名
      obj.path = path + '/' + file; //文件绝对路径
      filesList.push(obj);
    }
  }
}
function geFileTotal (path) {
  var filesList = [];
  readFile(path, filesList);
  console.log(filesList)
  var res = filesList.reduce((totalSize, item) => {
    return totalSize + item.size
  }, 0)
  console.log(res / 1024 + 'kb')
}
geFileTotal('./')
